package cn.hhy.hrm.service.Impl;

import cn.hhy.hrm.Exception.BasicException;
import cn.hhy.hrm.Exception.ErrorCode;
import cn.hhy.hrm.Exception.ValidUtils;
import cn.hhy.hrm.service.IVerifycodeService;
import cn.hhy.hrm.util.BitStatesUtils;
import cn.hhy.hrm.util.StrUtils;
import cn.hhy.hrm.util.VerifyCodeUtils;

import cn.hhy.hrm.vo.MobileVerificationCodeVO;
import cn.hhy.hrm.vo.SendSmsCodeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class VerifycodeServiceImpl implements IVerifycodeService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public String imageCode(String key) {
        //1.判断key不为空
        ValidUtils.assertNotEmpty(key, ErrorCode.CODE_400_ILLEGAL_REQUEST);
        //2.生成一个图片验证码的值
        String complexRandomString = StrUtils.getComplexRandomString(6);

        //3.把验证码的值存储到Redis,以前台传入的key作为key

        redisTemplate.opsForValue().set(key,complexRandomString,5, TimeUnit.MINUTES);
        //4.把验证码的值合并到图片

        try {
            return VerifyCodeUtils.VerifyCode(130, 35, complexRandomString);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void sendSmsCode(SendSmsCodeVO sendSmsCodeVO) {
//1.参数判断
        String imageCode = sendSmsCodeVO.getImageCode();
        String imageCodeKey = sendSmsCodeVO.getImageCodeKey();
        String mobile = sendSmsCodeVO.getMobile();
        ValidUtils.assertNotEmpty(imageCode, ErrorCode.CODE_400_ILLEGAL_REQUEST);
        ValidUtils.assertNotEmpty(imageCodeKey, ErrorCode.CODE_400_ILLEGAL_REQUEST);
        ValidUtils.assertNotEmpty(mobile, ErrorCode.CODE_400_ILLEGAL_REQUEST);


//2.判断图片验证码：

// 2.1.根据前段传入的key去Redis获取图片验证码的值，
        String s = (String) redisTemplate.opsForValue().get(imageCodeKey);
        ValidUtils.assertNotEmpty(s, ErrorCode.CODE_400_ILLEGAL_REQUEST);

// 2.2.传入的图片验证码的值和Redis中的值比较
        if (!s.equalsIgnoreCase(imageCode)){
            throw new BasicException(ErrorCode.CODE_400_VERIFICATION_CODE_ERROR);
        }


//3.获取上一次短信验证码发送记录
        MobileVerificationCodeVO getLastCode = (MobileVerificationCodeVO) redisTemplate.opsForValue().get("sms:" + mobile);


//3.1.上一次有
        String randomString = null;
        if(getLastCode==null){
            randomString = StrUtils.getRandomString(6);
        }else {
            if ((System.currentTimeMillis()-getLastCode.getTime())/1000 >60){
                randomString = getLastCode.getCode();
            }else {
                throw new BasicException(ErrorCode.CODE_400_VERIFICATION_CODE_REQUEST_TOO_FAST);
            }

        }
//3.1.1.判断时候是否过了重发时间 ：当前时间 - 上一次发送时间 > 60s
//3.1.2.如果没过重发时间 - 报错
//3.1.3.如果过了重发时间 ，获取上一次验证码的值

//3.2.上一次没有
//3.2.1.创建一个手机验证码



//4.把手机验证码发送记录存储到Redis
        //key: sms:电话号码    value:时间，验证码
        MobileVerificationCodeVO mobileVerificationCodeVO = new MobileVerificationCodeVO(System.currentTimeMillis(), randomString);


        redisTemplate.opsForValue().set("sms:"+mobile,mobileVerificationCodeVO,5,TimeUnit.MINUTES);
        log.info("短信验证码为："+randomString);
//5.调用短信网关发送手机验证码
//6.存储验证码的发送记录到Mysql

    }
}
